package nio.channel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 读取对应文件的数据
 */
public class FileChannel2 {
    public static void main(String[] args) throws IOException {
        File file = new File("e:\\file.txt");
        /**
         * 创建输入流
         */
        FileInputStream fileInputStream = new FileInputStream(file);
        /**
         * 获取对应的通道
         */
        FileChannel fileChannel = fileInputStream.getChannel();
        /**
         * 创建缓冲区
         */
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
        /**
         * 将管道中数据读入到缓冲区
         */
        fileChannel.read(byteBuffer);

        System.out.println(new String(byteBuffer.array()));
    }
}
